package org.bonorsoft.modules.hospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.bonorsoft.modules.hospital.entity.Admitte;
import org.bonorsoft.modules.hospital.entity.ZzyjsView;
import org.bonorsoft.modules.hospital.mapper.ZzyjsViewMapper;
import org.bonorsoft.modules.hospital.service.IZzyjsViewService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @Description: His系统视图
 * @Author: WilliamYe
 * @Date:   2020-11-04
 * @Version: V1.0
 */
@Service
public class ZzyjsViewServiceImpl extends ServiceImpl<ZzyjsViewMapper, ZzyjsView> implements IZzyjsViewService {

    @Resource
    private AdmitteServiceImpl admitteService;

    @Override
    public void importData() {
        QueryWrapper<ZzyjsView> queryWrapper = new QueryWrapper<>();
        String prevMonth = getPrevMonth();
        queryWrapper.ge("pat_in_time",prevMonth);
        List<ZzyjsView> list = this.list(queryWrapper);
        List<Admitte> admitteList = new ArrayList<>();
        list.forEach(item -> {
            Admitte admitte = new Admitte();
            BeanUtil.copyProperties(item, admitte);
            admitteList.add(admitte);
        });
        admitteService.saveOrUpdateBatch(admitteList);
    }

    private String getPrevMonth(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);//得到前一个月
        return format.format(calendar.getTime());
    }
}
